<?php

namespace Health\Http\Requests;

use Illuminate\Http\JsonResponse;


class CategoryRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'category_name' => 'required',
            'sort' => 'required|integer|min:1|max:100',
            'status' => 'required|in:1,-1'
        ];
    }

    public function messages()
    {
        return [
            'category_name.required' => '请填写分类名称',

            'sort.required' => '请填写排序',
            'sort.integer' => '排序值能为数字',
            'sort.min' => '排序值范围为1到100',
            'sort.max' => '排序值范围为1到100',

            'status.required' => '请选择状态',
            'status.in' => '状态非法'

        ];
    }

    public function response(array $errors)
    {
        if ($this->ajax() || $this->wantsJson()) {
            return new JsonResponse(array('status' => 0, 'msg' => current($errors)[0]));
        }
        return $this->redirector->to($this->getRedirectUrl())
            ->withInput($this->except($this->dontFlash))
            ->withErrors($errors, $this->errorBag);
    }
}
